<?php
namespace jy_vote;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function max_get($replyid){
		//冠军赠送
		$reply=pdo_get('jy_vote_reply',array('id'=>$replyid,'uniacid'=>$_SESSION['uniacid']));
		if($reply['endtime']>time()){
			exi('活动还未结束','error');
		}
		if($reply['max_oid']){
			exi('投票冠军奖励已领取！','error');
		}
		$params=array('rid'=>$replyid,'uniacid'=>$_SESSION['uniacid']);
		$alllist=pdo_getall('jy_vote_opus',$params,array('id','rid','title','likes','basevotenums','baselikenums'));
			if($alllist){
				$sum=count($alllist);
				foreach($alllist as &$av){
					$jifen=pdo_getsum('jy_vote_sendlplog',array('oid'=>$av['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
					$av['votenums']=pdo_count('jy_vote_votelog',array('oid'=>$av['id'],'uniacid'=>$_SESSION['uniacid'],'is_shenhe'=>1));
					$av['votenums']=$av['basevotenums']+$av['votenums']+$jifen;
				}
		}
		paixu($alllist,'votenums','desc');
		$mid=$alllist[0]['mid'];
		if($reply['max_type']>0 && $reply['max_credit_fee']){
			cfc('core')->member_credit($mid,'credit'.$reply['max_type'],$reply['max_credit_fee'],1,'jy_vote','冠军奖励');
		}
		if($reply['max_type']>1 && $reply['max_couponid']){
			$this->model('coupon')->getcoupon($reply['max_couponid'],$mid);
		}
		pdo_update('jy_vote_reply',array('max_oid'=>$alllist[0]['id']),array('id'=>$replyid,'uniacid'=>$_SESSION['uniacid']));
	}
	public function date($time){
		$res=array(
			'day'=>0,
			'hour'=>0,
			'minute'=>0,
			'second'=>0,
		);
		
		if($time<=0){
			return $res;
		}
		$res['day']=intval($time/86400);
		$time=$time-$res['day']*86400;
		$res['hour']=intval($time/3600);
		$time=$time-$res['hour']*3600;
		$res['minute']=intval($time/60);
		$res['second']=$time-$res['minute']*60;
		return $res;
	}
	public function buy_credit3($payresult=array(),$r_id=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$buy_credit=$this->plugin_setting('buy_credit3');
		if($payresult['pay_type']=='wechat' && strpos($buy_credit['paytypes'],';1;') === false){
			return false;
		}
		if($payresult['pay_type']=='credit' && strpos($buy_credit['paytypes'],';2;') === false){
			return false;
		}
		if($payresult['pay_type']=='alipay' && strpos($buy_credit['paytypes'],';3;') === false){
			return false;
		}
		if($buy_credit['type']==1){
			//根据订单金额赠送积分
			if(!$buy_credit['options']){
				return true;
			}
			foreach($buy_credit['options'] as $item){
				if($payresult['fee']>=$item['start'] && $payresult['fee']<=$item['end']){
					$credit=intval($payresult['fee']*$item['credit']);
					model('core/member')->member_credit($mid,'credit'.$item['type'],$credit,true,'jy_vote','消费获得积分');
					break;
				}
			}
		}elseif($buy_credit['type']==2 && $r_id){
			//根据商品赠送虚拟币
			$credit=0;
			$goods_credit = pdo_get('jy_vote_credit',array('uniacid'=>$_SESSION['uniacid'],'id'=>$r_id));
			if(!$goods_credit['id']){
				return false;
			}
			if(substr($goods_credit['credit'],-1)=='%'){
				$goods_credit['credit']=($payresult['fee']*substr($goods_credit['credit'],0,-1))/100;
			}
			if($goods_credit['credit'] && $goods_credit['type']>1){
				$credit=intval($goods_credit['credit']);
				model('core/member')->member_credit($mid,'credit'.$goods_credit['type'],$credit,true,'jy_vote','参与活动获得积分');
			}
		}
		return true;
	}
}